﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MoreShaperRTD;

namespace hk_ztd2pwv
{
    class EpochZTD
    {
        public EpochTime epochTime;
        public double ztd;
        public EpochZTD()
        { }
        public EpochZTD(string line)
        {
            epochTime = new EpochTime(line.Substring(0, 23));
            ztd = Convert.ToDouble(line.Substring(27, 6));
        }
        public void Print()
        {
            Console.WriteLine(string.Format("{0} {1,0:0.0000}", epochTime.ToString(), ztd));
        }
    }
    class ReadZTD
    {
        public string filename;
        public List<EpochZTD> listEpochZTD = new List<EpochZTD>();
        public ReadZTD(string tropFileName)
        {
            filename = tropFileName;
            string[] lines = File.ReadAllLines(tropFileName);
            foreach (string line in lines)
            {
                if(line.StartsWith("$"))
                {
                    continue;
                }
                string newline = line.Replace("/", " ").Replace(":", " ");
                EpochZTD epochZTD = new EpochZTD(newline);
                listEpochZTD.Add(epochZTD);
            }
        }
        public ReadZTD()
        { }
        EpochZTD GetZTDNearst(EpochTime epochTime)
        {
            ///EpochTime tmpEpoch = new EpochTime();
            EpochZTD tmpZTD = new EpochZTD();
            double deltaTime = 9999;
            foreach(EpochZTD epochZTD  in listEpochZTD)
            {
                if (Math.Abs(epochZTD.epochTime.DeltaTime(epochTime)) < deltaTime)
                {
                    tmpZTD = epochZTD;
                }
            }
            return tmpZTD;
        }
        public EpochZTD GetZTDNearst(string strhms)
        {
            string [] fields = strhms.Replace(":", " ").Split(
                new char[0], StringSplitOptions.RemoveEmptyEntries);
            int hour = Convert.ToInt16(fields[0]);
            int minute = Convert.ToInt16(fields[1]);
            double sec = Convert.ToDouble(fields[2]); 
            int year = this.listEpochZTD[0].epochTime.year;
            int month = this.listEpochZTD[0].epochTime.month;
            int day = this.listEpochZTD[0].epochTime.day;
            EpochTime tmpEpochTime = new EpochTime(year, month, day, hour, minute, sec);
            return GetZTDNearst(tmpEpochTime);
        }
    }
}
